home *** CD-ROM | disk | FTP | other *** search
- #!/usr/bin/env python
- """twdt -- Build the TWDT files (TWDT.html and TWDT.txt.gz) for a Linux Gazette
- issue (or issues). Run "twdt --help" for usage.
-
- This program is not guaranteed for issues below #85 (lg.twdtCutoff), and
- will abort if forced to do an older issue. There are hand-made files in CVS
- for the older issues. (Before issue #72 the files were named issue##.html and
- issue##.tar.gz instead of TWDT.html and TWDT.tar.gz.)
- """
- import gzip, os, re, sys
- sys.path.insert(0, '/home/lg/lib/python')
- from optik import OptionParser
- import lg
- from lg import debug, die
-
- try:
- True, False
- except NameError: # Python < 2.3
- True, False = (1==1), (1==0)
-
- USAGE = """\
- Usage: %prog [--all] [--issue=##] [--stdout]
- Builds the TWDT.html and TWDT.txt.gz files. If no arguments, use the current
- directory to deduce the issue number, read the source files and write the
- destination files."""
-
- HTML_FILE = "TWDT.html"
- TEXT_FILE = "TWDT.txt.gz"
- LG_ANSWER_DIR = "/home/lg/data/twdt/"
- CUT_RX = re.compile( R"<!--startcut[-= ].*?<!--endcut[-= ]*-->\n",
- re.DOTALL)
-
- lg.DEBUG = True
-
-
- def catArticleBody(out, sourceFile):
- """Read the article text from 'sourceFile', delete portions surrounded by
- "startcut ... endcut" comments, and write the result to file handle
- 'out'.
- """
- f = open(sourceFile, 'r')
- text = f.read()
- f.close()
- textCooked = CUT_RX.sub("", text)
- out.write(textCooked)
-
-
- def makeHTML(out, issue, dir):
- """Write TWDT.html for issue# 'issue' to the open file object 'out',
- reading the article files from directory 'dir'. If 'dir' is "", use
- the current directory.
- """
- debug("issue# %d" % issue)
- path = os.path.join(dir, "index.html")
- catArticleBody(out, path)
- for art in lg.issues[issue].articles:
- if art.key == "lg_answer":
- path = os.path.join(LG_ANSWER_DIR, "TWDT.lg_answer%d.html" % issue)
- else:
- path = os.path.join(dir, art.key + ".html")
- debug("%-20s from %s" % (art.key, path))
- catArticleBody(out, path)
- out.write("</BODY></HTML>\n")
-
-
-
-
- def makeText(dir):
- """Make TWDT.txt.gz from TWDT.html.
- Exc: AssertionError, if TWDT.html doesn't exist.
- """
- htmlFile = os.path.join(dir, HTML_FILE)
- textFile = os.path.join(dir, TEXT_FILE)
- assert os.path.exists(htmlFile)
- cmd = "links -dump %s | gzip >| %s" % (htmlFile, textFile)
- debug("Running subcommand: " + cmd)
- os.system(cmd)
-
- # Check for nulls.
- f = gzip.open(textFile)
- text = f.read()
- f.close()
- if '\0' in text:
- print "Warning: NULL character found in %s!" % textFile
-
-
- def processIssue(issue, dir, stdout):
- if stdout:
- out = sys.stdout
- else:
- path = os.path.join(dir, HTML_FILE)
- out = file(path, 'w')
- makeHTML(out, issue, dir)
- out.close()
- if not stdout:
- makeText(dir)
-
-
- def getIssueDir(issue):
- return "/LG/www/issue%d" % issue
-
- def getLG_AnswerPath(issue, dir):
- return "%s/TWDT.lg_answer%d.html" % (issue, dir)
-
- #### TOP-LEVEL OPERATIONS
- def doAll():
- issues = range(lg.currentIssue, lg.twdtCutoff - 1, -1)
- debug("Issues are %s" % issues)
- for issue in issues:
- debug("=== Issue %d" % issue)
- dir = getIssueDir(issue)
- processIssue(issue, dir, False)
-
- def doIssue(issue, stdout):
- print "Issue# %d" % issue
- dir = "/LG/www/issue%d" % issue
- processIssue(issue, dir, stdout)
-
- def doDefault(stdout):
- issue = lg.guessIssue()
- processIssue(issue, "", stdout)
-
-
- #### MAIN ROUTINE
- def main():
- op = OptionParser(USAGE)
- op.add_option("--all", action="store_true", dest="all", default=0,
- help="all issues from 85 to current, directory /LG/www/issue##")
- op.add_option("--issue", action="store", dest="issue", default=None,
- type="int", help="do that issue (integer), directory /LG/www/issue##/")
- op.add_option("--stdout", action="store_true", dest="stdout", default=0,
- help="write TWDT.html to standard output; do not write TWDT.tar.gz")
- options, args = op.parse_args()
- if len(args) != 0:
- op.error("too many command-line arguments")
- if options.all:
- if options.issue:
- op.error("can't do --all and --issue simultaneously")
- elif options.stdout:
- op.error("can't do --all and --stdout simultaneously")
- doAll()
- elif options.issue:
- doIssue(options.issue, options.stdout)
- else:
- doDefault(options.stdout)
-
-
-
-
- if __name__ == "__main__": main()
-
- # vim: sw=4 ts=4 expandtab
-